"""
数据模型模块
定义API请求和响应的数据模型
"""

from typing import Optional, List
from pydantic import BaseModel, Field, validator

# 使用绝对导入，只在backend目录设置__init__.py
from backend.config.constants import ExamConstants


class GenerateRequest(BaseModel):
    """题目生成请求模型"""
    grade: str = Field(..., description="年级")
    difficulty: str = Field(..., description="难度")
    subject: str = Field(..., description="科目")
    knowledge_points: List[str] = Field(..., description="知识点列表")
    question_type: str = Field(..., description="题型")
    num_questions: int = Field(default=5, ge=1, le=50, description="题目数量")
    
    @validator('grade')
    def validate_grade(cls, v):
        if v not in ExamConstants.GRADES:
            raise ValueError(f'不支持的年级: {v}')
        return v
    
    @validator('difficulty')
    def validate_difficulty(cls, v):
        if v not in ExamConstants.DIFFICULTIES:
            raise ValueError(f'不支持的难度: {v}')
        return v
    
    @validator('subject')
    def validate_subject(cls, v):
        if v not in ExamConstants.SUBJECTS:
            raise ValueError(f'不支持的科目: {v}')
        return v
    
    @validator('question_type')
    def validate_question_type(cls, v):
        if v not in ExamConstants.QUESTION_TYPES:
            raise ValueError(f'不支持的题型: {v}')
        return v
    
    @validator('knowledge_points')
    def validate_knowledge_points(cls, v):
        if not v:
            raise ValueError('知识点不能为空')
        return v
    
    class Config:
        json_schema_extra = {
            "example": {
                "grade": "初一",
                "difficulty": "中等",
                "subject": "数学",
                "knowledge_points": ["代数式", "一元一次方程"],
                "question_type": "选择题",
                "num_questions": 5
            }
        }


class StreamGenerateRequest(BaseModel):
    """流式生成请求模型"""
    grade: str = Field(..., description="年级")
    difficulty: str = Field(..., description="难度")
    subject: str = Field(..., description="科目")
    knowledge_points: List[str] = Field(..., description="知识点列表")
    question_type: str = Field(..., description="题型")
    num_questions: int = Field(default=5, ge=1, le=50, description="题目数量")
    
    @validator('grade')
    def validate_grade(cls, v):
        if v not in ExamConstants.GRADES:
            raise ValueError(f'不支持的年级: {v}')
        return v
    
    @validator('difficulty')
    def validate_difficulty(cls, v):
        if v not in ExamConstants.DIFFICULTIES:
            raise ValueError(f'不支持的难度: {v}')
        return v
    
    @validator('subject')
    def validate_subject(cls, v):
        if v not in ExamConstants.SUBJECTS:
            raise ValueError(f'不支持的科目: {v}')
        return v
    
    @validator('question_type')
    def validate_question_type(cls, v):
        if v not in ExamConstants.QUESTION_TYPES:
            raise ValueError(f'不支持的题型: {v}')
        return v
    
    @validator('knowledge_points')
    def validate_knowledge_points(cls, v):
        if not v:
            raise ValueError('知识点不能为空')
        return v
    
    class Config:
        json_schema_extra = {
            "example": {
                "grade": "初一",
                "difficulty": "中等",
                "subject": "数学",
                "knowledge_points": ["代数式", "一元一次方程"],
                "question_type": "选择题",
                "num_questions": 5
            }
        }


class ExportRequest(BaseModel):
    """Word导出请求模型"""
    data: str = Field(..., description="试卷数据JSON字符串")
    filename: Optional[str] = Field(default="exam.docx", description="文件名")
    include_answers: Optional[bool] = Field(default=True, description="是否包含答案")
    
    class Config:
        json_schema_extra = {
            "example": {
                "data": '{"grade":"初一","questions":[...]}',
                "filename": "math_exam.docx",
                "include_answers": True
            }
        }


class Question(BaseModel):
    """题目模型"""
    type: str = Field(..., description="题型")
    knowledge_point: str = Field(..., description="知识点")
    question: str = Field(..., description="题目内容")
    options: Optional[List[str]] = Field(default=None, description="选项（选择题专用）")
    answer: str = Field(..., description="答案")
    explanation: str = Field(..., description="解析")


class ExamData(BaseModel):
    """试卷数据模型"""
    grade: str = Field(..., description="年级")
    difficulty: str = Field(..., description="难度")
    subject: str = Field(..., description="科目")
    questions: List[Question] = Field(..., description="题目列表")


class GenerateResponse(BaseModel):
    """生成响应模型"""
    data: str = Field(..., description="生成的数据")
    status: str = Field(default="success", description="状态")


class ConfigResponse(BaseModel):
    """配置响应模型"""
    grades: List[str] = Field(..., description="支持的年级")
    difficulties: List[str] = Field(..., description="支持的难度")
    subjects: List[str] = Field(..., description="支持的科目")
    question_types: List[str] = Field(..., description="支持的题型")
    knowledge_points: dict = Field(..., description="知识点配置")
    max_questions: int = Field(..., description="最大题目数量")


class HealthResponse(BaseModel):
    """健康检查响应模型"""
    status: str = Field(default="healthy", description="状态")
    service: str = Field(..., description="服务名称")
    version: str = Field(..., description="版本")
    model: str = Field(..., description="使用的模型")